package com.huanghuai.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * 用户详情服务接口定义一个bean
 */
@Configuration
@Slf4j
public class MySecurityUserConfig {

    @Bean
    public UserDetailsService userDetailsService(){
        //创建了2个用户
        UserDetails user1 =  User.builder()
                        .username("eric")
                        .password(passwordEncoder().encode("123456"))
                        .roles("student") //在角色前面加上ROLE_student就变成了权限
                        .authorities("student:delete","student:add")//配置用户权限
                        .build();
        UserDetails user2 =  User.builder()
                        .username("Obama")
                        .password(passwordEncoder().encode("123456"))
                        .roles("teacher")
                        .authorities("teacher:query")//配置用户权限
                        .build();
        UserDetails user3 =  User.builder()
                .username("admin")
                .password(passwordEncoder().encode("123456"))
                .roles("admin")
                .authorities("teacher:query","teacher:add","teacher:delete","teacher:update")//配置用户权限
                .build();
        //用户细节管理器
        InMemoryUserDetailsManager userDetailsService = new InMemoryUserDetailsManager();
        userDetailsService.createUser(user1);
        userDetailsService.createUser(user2);
        userDetailsService.createUser(user3);
        return userDetailsService;
    }

    /**
     * 配置密码编码器，NoOpPasswordEncoder.getInstance()此方法为不加密
     * @return
     */
    /**
     * zi
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
